package com.jacobson.huobi.mvp

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.jacobson.core.base.Immersive
import com.jacobson.core.di.components.AppComponent
import com.jacobson.core.di.modules.ActivityModule
import com.jacobson.core.framework.annotation.IntentParameter
import com.jacobson.core.mvp.BaseMvpActivity
import com.jacobson.huobi.R
import com.jacobson.huobi.components.SpaceItemDecoration
import com.jacobson.huobi.di.components.DaggerAlarmRecordActivityComponent
import com.jacobson.huobi.di.modules.AlarmRecordActivityModule
import com.jacobson.huobi.entities.SymbolRecordInfo
import com.jacobson.huobi.entities.response.AlarmRecordPageResponse
import com.jacobson.huobi.mvp.adapters.SymbolRecordAdapter
import com.jacobson.huobi.mvp.contract.AlarmRecordContract
import com.jacobson.huobi.utils.ResourcesUtil
import kotlinx.android.synthetic.main.fragment_currency.*
import kotlinx.android.synthetic.main.fragment_currency.commonHeader
import kotlinx.android.synthetic.main.fragment_home.*
import javax.inject.Inject

@Immersive(fitSystemWindows = false)
class AlarmRecordActivity :
    BaseMvpActivity<AlarmRecordContract.Presenter>(), AlarmRecordContract.View {
    private var lastTs: Long? = null
    @field:IntentParameter(KEY_SYMBOL)
    private lateinit var symbol: String
    @field:IntentParameter(KEY_PERIOD)
    private lateinit var period: String

    @field:Inject
    lateinit var symbolRecordAdapter: SymbolRecordAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //修改底部功能键背景色 只支持api为21及以上
        if (Build.VERSION.SDK_INT  >= Build.VERSION_CODES.LOLLIPOP) {
            window.navigationBarColor = ResourcesUtil.getColor(R.color.colorPrimary) //设定底部导航栏  同全局颜色
        }
    }
    override fun setupActivityComponent(appComponent: AppComponent) {
        DaggerAlarmRecordActivityComponent.builder()
            .alarmRecordActivityModule(AlarmRecordActivityModule(this))
            .appComponent(appComponent)
            .activityModule(ActivityModule(this))
            .build()
            .inject(this)
    }

    override fun initView(savedInstanceState: Bundle?): Int = R.layout.activity_alarm_record

    override fun initData(savedInstanceState: Bundle?) {
        logger.d("list record symbol = $symbol, period = $period")
        commonHeader.setForcePadding(true)
        with(recyclerView) {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(3f))
            adapter = symbolRecordAdapter
        }
        with(smartRefreshLayout) {
            setOnRefreshListener {
                loadSymbolRecordList(true)
            }
            setOnLoadMoreListener {
                loadSymbolRecordList(false)
            }
        }
        loadSymbolRecordList()
    }

    override fun onError(type: Int, extra: Any?, code: Int, message: String): Boolean {
        smartRefreshLayout.finishLoadMore()
        smartRefreshLayout.finishRefresh()
        logger.e("error >>> $message")
        return super.onError(type, extra, code, message)
    }

    private fun loadSymbolRecordList(refresh: Boolean = false) {
        if (refresh) {
            lastTs = null
        }
        presenter.loadSymbolRecordList("huobi", symbol, 0, period, lastTs)
    }

    @Synchronized
    override fun onSymbolRecordResp(response: AlarmRecordPageResponse) {
        smartRefreshLayout.finishLoadMore()
        smartRefreshLayout.finishRefresh()
        symbolRecordAdapter.setPeriod(period)
        if (lastTs == null && response.reqTs == -1L) {
            lastTs = response.ts
            symbolRecordAdapter.update(response.data)
        } else if (lastTs == response.reqTs) {
            lastTs = response.ts
            symbolRecordAdapter.append(response.data)
        }
    }

    companion object {
        private const val KEY_SYMBOL = "key_symbol"
        private const val KEY_PERIOD = "key_period"
        fun startActivity(context: Context, symbol: String, period: String) {
            Intent(context, AlarmRecordActivity::class.java).run {
                putExtra(KEY_SYMBOL, symbol)
                putExtra(KEY_PERIOD, period)
                context.startActivity(this)
            }
        }
    }
}
